package com.capacitydorm.server.service;

import com.capacitydorm.server.common.vo.DormVo;
import com.capacitydorm.server.common.vo.FloorVo;
import com.capacitydorm.server.common.vo.NumVo;
import com.capacitydorm.server.dao.DormMapper;
import com.capacitydorm.server.dao.FloorMapper;
import com.capacitydorm.server.dao.StudentMapper;
import com.capacitydorm.server.dao.TowerMapper;
import com.capacitydorm.server.domain.Dorm;
import com.capacitydorm.server.domain.Floor;
import com.capacitydorm.server.domain.Student;
import com.capacitydorm.server.domain.Tower;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DormService {
    @Resource
    private DormMapper dormMapper;

    @Resource
    private FloorMapper floorMapper;

    @Resource
    private TowerMapper towerMapper;

    @Resource
    private StudentMapper studentMapper;

    public void addDorm(Dorm dorm) {

        dormMapper.insert(dorm);
    }

    public void delectDormById(Integer id) {

        dormMapper.deleteByPrimaryKey(id);
    }

    public void updateDormById(Dorm dorm)
    {
        dormMapper.updateByPrimaryKey(dorm);
    }

    public Dorm checkDormById(Integer id) {
        Dorm dorm = dormMapper.findById(id);

        List<Student> byDormId = studentMapper.findByDId(dorm.getId());
        dorm.setStudents(byDormId);
        return dorm;
    }

    public Dorm checkOneDormByEx(String doorword) {
        Dorm byNum = dormMapper.findByNum(doorword);
        List<Student> byDId = studentMapper.findByDId(byNum.getId());
        byNum.setStudents(byDId);
        return byNum;

    }

    public List<Dorm> checkDormsByFloorId(Integer id) {
        Example example = new Example(Dorm.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("floorId", id);
        return dormMapper.selectByExample(example);

    }

    public DormVo findAll() {
        DormVo dormVo = new DormVo();
        List<Tower> towers = towerMapper.selectAll();
        List<Floor> floors = floorMapper.selectAll();
        List<Dorm> dorms = dormMapper.selectAll();

        dormVo.setHouses(towers);

        Map<String, List<FloorVo>> map = new HashMap<>();

        for (Tower t : towers) {
            List<FloorVo> list1 = new ArrayList<>();
            for (Floor f : floors) {
                if (f.getTowerId().equals(t.getId())) {
                    FloorVo floorVo = new FloorVo();
                    floorVo.setHouse(t.getName());
                    floorVo.setId(f.getId() + "");
                    floorVo.setName(f.getNumber());
                    list1.add(floorVo);
                }
            }
            map.put(t.getId() + "", list1);
        }
        dormVo.setFloors(map);

        Map<String, List<NumVo>> map1 = new HashMap<>();
        for (Floor f :floors) {
            List<NumVo> list = new ArrayList<>();
            for (Dorm d:dorms) {
                if (d.getFloorId().equals(f.getId())){
                    NumVo numVo = new NumVo();
                    numVo.setId(d.getId()+"");
                    numVo.setFloor(f.getNumber());
                    numVo.setName(d.getNumber());
                    list.add(numVo);
                }
            }
            map1.put(f.getId()+"",list);
        }
        dormVo.setNums(map1);
        return dormVo;
    }

    public List<Dorm> check(Dorm dorm) {
       return dormMapper.select(dorm);
    }
}
